"use strict";
document.addEventListener("mousedown", function (event) {
    if (event.button === 2) {
        chrome.runtime.sendMessage({url: getBackgroundImage(event.target)});
    }
});

function getBackgroundImage(target) {
    var backgroundImage, match, image;
    do {
        backgroundImage = window.getComputedStyle(target, null).getPropertyValue("background-image");
        if (backgroundImage !== "none") {
            match = backgroundImage.match(/https?:\/\/.+?\.(jpg|png|gif])/i);
            if (match !== null) {
                image = match[0];
                break;
            }
        }
        target = target.parentElement;
    } while (target != null);
    return image;
}